package com.soft.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.soft.admin.common.ElementConst;
import com.soft.admin.dto.CentreDTO;
import com.soft.admin.dto.ComboDTO;
import com.soft.admin.service.CentreService;
import com.soft.dao.CentreDao;
import com.soft.model.Centre;
import com.soft.model.Channel;
import com.soft.support.PageParam;
import com.soft.support.Pager;
import com.soft.util.DateUtil;
import com.soft.util.JsonUtil;

@SuppressWarnings("unchecked")
@Component
public class CentreServiceImpl implements CentreService {
	private CentreDao centreDao;

	@Autowired
	public void setCentreDao(CentreDao centreDao) {
		this.centreDao = centreDao;
	}
	
	@Transactional(readOnly = true)
	public String get(CentreDTO params, PageParam page) {
		List<Criterion> values = new ArrayList<Criterion>();
		values.add(Restrictions.like("title", params.getTitle(), MatchMode.ANYWHERE));
		if (params.getChannel() != -1) {
			values.add(Restrictions.eq("channel.id", params.getChannel()));
		}
		Pager centreLst = this.centreDao.findForPager(Centre.class, 
				new Order[] { Order.desc("id") }, 
				page.getStart(), page.getLimit(), values);
		Pager pageModel = new Pager();
		pageModel.setTotal(centreLst.getTotal());
		List<CentreDTO> dtos = new ArrayList<CentreDTO>();
		for (Centre centre : (List<Centre>)centreLst.getDatas()) {
			CentreDTO dto = new CentreDTO();
			BeanUtils.copyProperties(centre, dto, new String[]{"channel",  "updateDate"});
			dto.setChannel(centre.getChannel().getId());
			dto.setUpdateDate(DateUtil.format(centre.getUpdateDate()));
			dtos.add(dto);
		}
		pageModel.setDatas(dtos);
		return JsonUtil.toStringFromObject(pageModel);
	}

	@Transactional
	public String addOrEdit(CentreDTO params) {
		Centre centre = (Centre)this.centreDao.get(Centre.class, params.getId());
		centre.setUpdateDate(new Date());
		centre.setTitle(params.getTitle());
		centre.setTxt(params.getTxt());
		this.centreDao.addOrEdit(centre);
		return JsonUtil.toRes("保存成功！");
	}

	@Transactional
	public String remove(Integer id) {
		Centre team = (Centre)this.centreDao.get(Centre.class, id);
		this.centreDao.remove(team);
		return JsonUtil.toRes("删除成功！");
	}

	@Transactional(readOnly = true)
	public String getChannel() {
		Channel chan = (Channel)this.centreDao.findUniqueByProperty(Channel.class, "namespace", ElementConst.ZXJJCONST);
		List<Channel> channels = this.centreDao.findAll(Channel.class, null, Restrictions.eq("parent.id", chan.getId()));
		List<ComboDTO> dtos = new ArrayList<ComboDTO>();
		for (Channel channel : channels) {
			ComboDTO dto = new ComboDTO();
			dto.setId(channel.getId());
			dto.setStr(channel.getName());
			dtos.add(dto);
		}
		return JsonUtil.toString(dtos);
	}
}
